<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>JSON实体整合工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f0f0f0;
        }
        .container {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .input-box {
            flex: 1;
            background-color: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        textarea {
            width: 100%;
            height: 300px;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            font-family: Consolas, monospace;
        }
        button {
            display: block;
            margin: 0 auto 20px;
            padding: 12px 24px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            background-color: #f8f8f8;
            padding: 15px;
            border-radius: 4px;
            max-height: 500px;
            overflow-y: auto;
        }
        h2 {
            color: #333;
            margin-top: 0;
        }
    </style>
</head>
<body>
    <h1 style="text-align: center; color: #333;">JSON实体整合工具</h1>
    
    <div class="container">
        <div class="input-box">
            <h2>第一个Entity.json</h2>
            <textarea id="json1" placeholder="在此粘贴第一个JSON内容..."></textarea>
        </div>
        <div class="input-box">
            <h2>第二个Entity.json</h2>
            <textarea id="json2" placeholder="在此粘贴第二个JSON内容..."></textarea>
        </div>
    </div>

    <button onclick="mergeJson()">整合JSON数据</button>

    <div id="result">
        <h2>整合结果</h2>
        <pre id="output"></pre>
    </div>

    <script>
        function mergeJson() {
            try {
                // 获取输入内容
                const json1 = JSON.parse(document.getElementById('json1').value);
                const json2 = JSON.parse(document.getElementById('json2').value);

                // 创建合并后的对象
                const merged = {
                    ...json1,
                    ...json2,
                    entities: mergeEntities(json1.entities || [], json2.entities || [])
                };

                // 显示美化后的结果
                document.getElementById('output').textContent = JSON.stringify(merged, null, 4);
            } catch (error) {
                alert(`解析错误: ${error.message}`);
            }
        }

        function mergeEntities(entities1, entities2) {
            const entityMap = new Map();

            // 合并并处理重复项（后者覆盖前者）
            [...entities1, ...entities2].forEach(entity => {
                if (entity.name) {
                    entityMap.set(entity.name, entity);
                }
            });

            return Array.from(entityMap.values());
        }
    </script>
</body>
</html>